package cn.itcast.user.web;

import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import cn.itcast.user.vo.DateformateVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope // 1.开启配置自动更新
public class UserController {

    @Autowired
    private UserService userService;
    //1.读取配置文件内容
//    @Value("${pattern.dateformat}")
//    private String dateformat;
    //2.
    @Autowired
    private DateformateVO dateformateVO;

    @Value("${userInfo.name}")
    private String name;

    @GetMapping("now")
    public String now(){
//        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformateVO.getDateformat())) + name;
    }

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        return userService.queryById(id);
    }

    /**
     * 测试网关是否移除cookie
     * @param cookie
     * @return
     */
    @GetMapping("/getcookie")
    public String getHead(@RequestHeader(value = "Cookie",required = false) String cookie){
        System.out.println("cookie = " + cookie);
        return cookie;
    }

    /**
     * openfeign的多参数测试
     */
    @GetMapping("/getuser")
    public String getUser(@RequestParam("id") Long id, @RequestParam("name") String name){
        return id + ":" + name;
    }
}
